package com.spark.graph

import org.apache.spark.{SparkConf, SparkContext}
import org.apache.spark.graphx.Graph

/**
  * Created by Administrator on 2017/7/27.
  */
object graphMain {

  def main(args: Array[String]): Unit = {


    val conf = new SparkConf().setMaster("local").setAppName("graph")
    val sc = new SparkContext(conf)

    val rdd=sc.parallelize(List((1,"a"),(2,"b"),(3,"c"),(4,"d"),(5,"e"),(6,"f")))

    val initRdd=rdd.map(x=>(x._1.toLong,x._2))

    val list = List((1, 2), (1, 3), (1, 4), (3, 4), (3, 5),(3, 6), (5, 6))


    val vertexrdd = sc.parallelize(list).map(x=>(x._1.toLong,x._2.toLong))
    val graph = Graph.fromEdgeTuples(vertexrdd, 1)
    val graphots = Graph.graphToGraphOps(graph).connectedComponents().vertices


   val res= initRdd.join(graphots)
    val simrddtop = res.groupByKey().sortBy(-_._2.size)

    simrddtop.foreach(x=>println(x))





  }

}
